import { useUserStore } from '@/Store/userStore';
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';


const routes: Array<RouteRecordRaw> = [
    // {
    //   path: "/404", // 404页面路径
    //   name: "404",
    //   component: () => import("@/views/error.vue"), // 404页面组件
    // },
    {
        path: '/',
        redirect: '/index',
    },
    {
        path: '/login',
        name: '注册',
        component: () => import('@/pages/login/login.vue')
    },
    {
        path: '/index',
        name: 'index',
        component: () => import('@/pages/index/home.vue')
    },
    // {
    //     path: '/home',
    //     name: 'home',
    //     component: () => import('@/pages/index/home.vue')
    // },
    {
        path: '/test',
        name: '测试',
        component: () => import('@/pages/test/test.vue')
    },
    {
        path: '/drainage',
        name: '排水',
        component: () => import('@/layouts/smartDrainage.vue'),
        redirect: '/drainage/index',
        children: [
            {
                path: 'index',
                name: '排水1',
                component: () => import('@/pages/drainage/index.vue')
            }
        ]
    },

    {
        path: '/water',
        name: '水务',
        component: () => import('@/layouts/waterLayout.vue'),
        children: [
            {
                path: 'index',
                name: '供水首页',
                component: () => import('@/pages/water/index.vue')
            },
            {
                path: 'monitor',
                name: '供水管网监控',
                component: () =>
                    import('@/pages/water/WaterSupplyMonitoring.vue')
            },
            {
                path: 'fireMonitor',
                name: '消防管网监控',
                component: () =>
                    import('@/pages/water/FireHydrantMonitoring.vue')
            },
            {
                path: 'equipment',
                name: '设施设备管理',
                component: () => import('@/pages/water/DevManage.vue')
            },
            {
                path: 'waterAnalysis',
                name: '水务数据分析',
                component: () => import('@/pages/water/WaterDataAnalytics.vue')
            },
            {
                path: 'watersavingAnalysis',
                name: '节水分析',
                component: () => import('@/pages/water/WaterSavingAnalysis.vue')
            },
            {
                path: 'watersourceMng',
                name: '水源管理',
                component: () =>
                    import('@/pages/water/WaterSourceManagement.vue')
            }
        ]
    },
    {
        path: '/green',
        name: '绿化',
        component: () => import('@/layouts/smartGreen.vue'),
        redirect: '/green/index',
        children: [
            {
                path: 'index',
                name: '地图模式',
                component: () => import('@/pages/green/index.vue')
            },
            {
                path: 'auto',
                name: '自动灌溉',
                component: () => import('@/pages/green/AutoIrrigation.vue')
            },
            {
                path: 'timed',
                name: '定时灌溉',
                component: () => import('@/pages/green/TimedIrrigation.vue')
            },
            {
                path: 'circular',
                name: ' 循环灌溉',
                component: () => import('@/pages/green/CircularIrrigation.vue')
            }
        ]
    },
    {
        path: '/power',
        name: '电力',
        component: () => import('@/layouts/smartPower.vue'),
        redirect: '/power/index',
        children: [
            {
                path: 'index',
                name: '用电信息',
                component: () => import('@/pages/power/index.vue')
            },
            {
                path: 'energyAnalaysis',
                name: '用电分析',
                component: () => import('@/pages/power/index.vue')
            },
            {
                path: 'dataReport',
                name: '数据报表',
                component: () => import('@/pages/power/index.vue')
            },
            {
                path: 'energyContrl',
                name: '节电管控',
                component: () => import('@/pages/power/energyContrl.vue')
            }
        ]
    },
    {
        path: '/pay',
        name: '缴费',
        component: () => import('@/pages/power/pay.vue')
    },
    {
        path: '/savew',
        name: '节水',
        component: () => import('@/layouts/waterSave.vue'),
        redirect: '/savew/index',
        children: [
            {
                path: 'index',
                name: '节水1',
                component: () => import('@/pages/save/index.vue')
            },
            {
                path: 'index1',
                name: '夜间流量分析',
                component: () => import('@/pages/save/index.vue')
            },
            {
                path: 'index2',
                name: '管理制度建设',
                component: () => import('@/pages/save/index.vue')
            }
        ]
    },

    {
        path: '/repair',
        name: '报修',
        component: () => import('@/layouts/repairLayout.vue'),
        redirect: '/repair/index',
        children: [
            {
                path: 'index',
                name: '最新申报',
                component: () => import('@/pages/repair/RepairView.vue')
            },
            {
                path: 'repair_detail',
                name: '数据汇总',
                component: () => import('@/pages/repair/RepairDetail.vue')
            },
            {
                path: 'repair_Person',
                name: '个人效率',
                component: () => import('@/pages/repair/RepairPerson.vue')
            },
            {
                path: 'repair_Satisfaction',
                name: '满意度',
                component: () => import('@/pages/repair/RepairSatisfaction.vue')
            },
            {
                path: 'repair_Accept',
                name: '受理效率',
                component: () => import('@/pages/repair/RepairAccept.vue')
            }
        ]
    },
    {
        path: '/edit',
        name: '编辑',
        component: () => import('@/pages/edit/EditView.vue')
    }

    // { path: "*", redirect: "/404" },
];

const router = createRouter({
    history: createWebHashHistory(),
    routes
});

const whiteList = ['/official', '/login', '/config', '/expire'];
router.beforeEach(async (to, from, next) => {
  const userStore = useUserStore();
  if (userStore.token) {
    next();
  } else {
    if(to.path !== '/login' && router.currentRoute.value.path !== '/login'){
        next({ path: '/login' });
    } else {
        next();
    }
  }
});

// 在这里添加路由的导航守卫

export default router;
